package com.xzit.headline.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.j256.ormlite.dao.Dao;
import com.xzit.headline.R;
import com.xzit.headline.bean.NewListData;
import com.xzit.headline.db.MyDbHelper;
import com.xzit.headline.db.NewInfo;

import java.sql.SQLException;
import java.util.List;

import static com.xzit.headline.fragment.BaseFragment.HOST;

/**
 * 8.新闻列表适配器
 */
public class FavouriteListAdapter extends BaseAdapter {
    private List<NewListData.DataBean.NewsBean> listData;


    public List<NewListData.DataBean.NewsBean> getListData() {
        return listData;
    }

    private Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    /**
     * 构造方法
     *
     * @param listData
     * @param context
     */
    public FavouriteListAdapter(List<NewListData.DataBean.NewsBean> listData, Context context) {
        this.listData = listData;
        this.context = context;
    }

    @Override
    public int getCount() {//行数
        return listData.size();
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {//哪一行是三图的,哪一行是一图的
        NewListData.DataBean.NewsBean bean = listData.get(position);
        if (bean.type == 0) {
            //一图
            return 0;
        } else {
            //三图
            return 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {//解决视图问题

        //从服务器获取数据
        NewListData.DataBean.NewsBean newsBean = listData.get(position);//每一条数据都是NewsBean

        boolean isExist = false;

        //判断当前视图的类型是一图还是三图
        int type = getItemViewType(position);

        /**
         * 判断是一图还是三图
         */
        if (type == 0) {//一图执行
            convertView = setDataToOneView(convertView, newsBean, isExist);
            return convertView;
        } else {//三图执行
            convertView = setDataToThreeView(convertView, newsBean, isExist);
            return convertView;
        }
    }

    /**
     * 给一图赋值
     *
     * @param convertView
     * @param newsBean
     * @param isExist
     * @return
     */
    private View setDataToOneView(View convertView, NewListData.DataBean.NewsBean newsBean, boolean isExist) {
        ViewHolderOne holderOne = null;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_new_one, null);
            holderOne = new ViewHolderOne();

            //初始化组件
            holderOne.title = convertView.findViewById(R.id.item_title);
            holderOne.date = convertView.findViewById(R.id.item_date);
            holderOne.image = convertView.findViewById(R.id.item_image);

            convertView.setTag(holderOne);
        } else {
            //重用,findViewById(),set..
            //convertView != null;
            holderOne = (ViewHolderOne) convertView.getTag();
        }

        //赋值标题和日期
        holderOne.title.setText(newsBean.title);
        holderOne.date.setText(newsBean.pubdate);

        //赋值图片
        String url = HOST + newsBean.listimage;
        Glide.with(context).load(url).into(holderOne.image);
        holderOne.image.setScaleType(ImageView.ScaleType.CENTER_CROP);


        //根据是否存在于数据库中,来调整标题和日期的颜色
        holderOne.title.setTextColor(isExist ? Color.GRAY : Color.BLACK);
        holderOne.date.setTextColor(isExist ? Color.GRAY : Color.BLACK);


        return convertView;
    }

    /**
     * 给三图赋值
     *
     * @param convertView
     * @param newsBean
     * @param isExist
     * @return
     */
    private View setDataToThreeView(View convertView, NewListData.DataBean.NewsBean newsBean, boolean isExist) {
        ViewHolderThree holderThree = null;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_new_three, null);
            holderThree = new ViewHolderThree();
            //初始化组件
            holderThree.title = convertView.findViewById(R.id.item_title);
            holderThree.date = convertView.findViewById(R.id.item_date);
            holderThree.image = convertView.findViewById(R.id.item_image);
            holderThree.image1 = convertView.findViewById(R.id.item_image1);
            holderThree.image2 = convertView.findViewById(R.id.item_image2);

            convertView.setTag(holderThree);
        } else {
            //重用,findViewById(),set..
            //convertView != null;
            holderThree = (ViewHolderThree) convertView.getTag();
        }

        //赋值标题和日期
        holderThree.title.setText(newsBean.title);
        holderThree.date.setText(newsBean.pubdate);

        //赋值图片
        String url = HOST + newsBean.listimage;
        String url1 = HOST + newsBean.listimage1;
        String url2 = HOST + newsBean.listimage2;
        Glide.with(context).load(url).into(holderThree.image);
        Glide.with(context).load(url1).into(holderThree.image1);
        Glide.with(context).load(url2).into(holderThree.image2);
        holderThree.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        holderThree.image1.setScaleType(ImageView.ScaleType.CENTER_CROP);
        holderThree.image2.setScaleType(ImageView.ScaleType.CENTER_CROP);

        //9.4根据是否存在于数据库中,来调整标题和日期的颜色
        holderThree.title.setTextColor(isExist ? Color.GRAY : Color.BLACK);
        holderThree.date.setTextColor(isExist ? Color.GRAY : Color.BLACK);

        return convertView;
    }
}
